package cn.iocoder.yudao.module.pmp.dal.mysql.project;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils;
import cn.iocoder.yudao.module.pmp.controller.admin.project.vo.ProjectPageReqVO;
import cn.iocoder.yudao.module.pmp.dal.dataobject.project.ProjectDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * 项目登记 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ProjectMapper extends BaseMapperX<ProjectDO> {

    default int updateOwnerUserIdById(Long id, Long ownerUserId) {
        return update(new LambdaUpdateWrapper<ProjectDO>()
                .eq(ProjectDO::getId, id)
                .set(ProjectDO::getOwnerUserId, ownerUserId));
    }

    default PageResult<ProjectDO> selectPage(ProjectPageReqVO reqVO, Long userId) {

        MPJLambdaWrapperX<ProjectDO> query = new MPJLambdaWrapperX<>();
        // 拼接数据权限的查询条件
        CrmPermissionUtils.appendPermissionCondition(query, CrmBizTypeEnum.CRM_RECEIVABLE_PMP.getType(),
                ProjectDO::getId, userId, reqVO.getSceneType());
        // 拼接自身的查询条件
        query.selectAll(ProjectDO.class)
                .likeIfPresent(ProjectDO::getName, reqVO.getName())
                .eqIfPresent(ProjectDO::getProjcetNo, reqVO.getProjcetNo())
                .eqIfPresent(ProjectDO::getCustomerId, reqVO.getCustomerId())
                .eqIfPresent(ProjectDO::getBusinessId, reqVO.getBusinessId())
                .eqIfPresent(ProjectDO::getContractId, reqVO.getContractId())
                .betweenIfPresent(ProjectDO::getContactLastTime, reqVO.getContactLastTime())
                .eqIfPresent(ProjectDO::getOwnerUserId, reqVO.getOwnerUserId())
                .eqIfPresent(ProjectDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(ProjectDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(ProjectDO::getJcDate, reqVO.getJcDate())
                .betweenIfPresent(ProjectDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ProjectDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(ProjectDO::getProjcetPrice, reqVO.getProjcetPrice())
                .eqIfPresent(ProjectDO::getDiscountPercent, reqVO.getDiscountPercent())
                .eqIfPresent(ProjectDO::getProjcetStatus, reqVO.getProjcetStatus())
                .eqIfPresent(ProjectDO::getSftqjc, reqVO.getSftqjc())
                .betweenIfPresent(ProjectDO::getTqjcDate, reqVO.getTqjcDate())
                .eqIfPresent(ProjectDO::getSignUserId, reqVO.getSignUserId())
                .eqIfPresent(ProjectDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(ProjectDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProjectDO::getId);
        return selectJoinPage(reqVO, ProjectDO.class, query);
    }

}
